package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/shopping")
@Slf4j
public class CartController {

    @Reference(check = false)
    ICartService cartService;

    @GetMapping("carts")
    public ResponseData carts(HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject jsonObject = JSONObject.parseObject(userInfo);
        Long uid = Long.parseLong(jsonObject.get("id").toString());
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);
        CartListByIdResponse response = cartService.getCartListById(cartListByIdRequest);
        if (ShoppingRetCode.SUCCESS.getMessage().equals(response.getMsg())) {
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg("fail");
    }


    @DeleteMapping("items/{id}")
    public ResponseData deleteCartByTwo(@PathVariable("id") String id){
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        long uid = Long.parseLong(id);
        deleteCheckedItemRequest.setUserId(uid);
        DeleteCheckedItemResposne resposne = cartService.deleteCheckedItem(deleteCheckedItemRequest);
        if (resposne.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(resposne.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg("fail");
    }


    @PostMapping("carts")
    public ResponseData addCart(@RequestBody Map<String,Object> map){

        AddCartRequest request = new AddCartRequest();
        String id = (String) map.get("userId");
        Integer userId = Integer.parseInt(id);
        Integer productId = (Integer) map.get("productId");
        request.setUserId(userId.longValue());
        request.setItemId(productId.longValue());
        request.setNum((Integer) map.get("productNum"));
        AddCartResponse response = cartService.addToCart(request);
        return new ResponseUtil<>().setData(response);
    }

    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCartsProduct(@PathVariable Long uid,@PathVariable Long pid){
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);
        DeleteCartItemResponse deleteCartItemResponse = cartService.deleteCartItem(deleteCartItemRequest);
        deleteCartItemResponse.setMsg("成功");
        return new ResponseUtil<>().setData(deleteCartItemResponse.getMsg());
    }
    @PutMapping("items")
    public ResponseData checkAllItems(@RequestBody Map<String,Object> map){
        CheckAllItemRequest request = new CheckAllItemRequest();
        String userId = (String) map.get("userId");
        Boolean checked = (Boolean) map.get("checked");
        String b = checked.toString();
        Long id = Long.parseLong(userId);
        request.setUserId(id);
        request.setChecked(b);
        CheckAllItemResponse response = cartService.checkAllCartItem(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

}
